﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AgileSaude.Data.Repository
{
    public class medicoRepository : ImedicoRepository
    {
        agilesaudeEntities entity;

        public medicoRepository()
        {
            entity = agilesaudeContext.getInstance();
        }

        public IQueryable<medico> Getmedicos()
        {
            return agilesaudeContext.getInstance().medico;
        }
        
        public IList<medico> GetAll()
        {
            return agilesaudeContext.getInstance().medico.ToList();
        }

        public medico getmedico(int pmedicoId)
        {
            return entity.medico.SingleOrDefault(a => a.idMedico == pmedicoId);
        }

        public void Delete(medico pmedico)
        {
            try
            {
                entity.medico.DeleteObject(pmedico);
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                agilesaudeContext.removeInstance();
                throw ex;
            }
        }

        public void Add(medico pmedico)
        {
            try
            {
                entity.AddTomedico(pmedico);
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                agilesaudeContext.removeInstance();
                throw ex;
            }
        }

        public void Update()
        {
            try
            {
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                agilesaudeContext.removeInstance();
                throw ex;
            }
        }

        public medico getmedico(string pNome)
        {
            return entity.medico.SingleOrDefault(a => a.Nome == pNome);
        }
    }
}
